<template>
  <div class="add">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加商品</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="box-card">
      <el-alert
        title="添加商品信息"
        type="info"
        center
        :closable="false"
        show-icon
      >
      </el-alert>
      <el-steps :active="active - 0" finish-status="success" align-center>
        <el-step title="基本信息"></el-step>
        <el-step title="商品参数"></el-step>
        <el-step title="商品属性"></el-step>
        <el-step title="商品图片"></el-step>
        <el-step title="商品内容"></el-step>
        <el-step title="完成"></el-step>
      </el-steps>

      <el-tabs v-model="active" tab-position="left" :before-leave="beforeLeave">
        <el-tab-pane label="基本信息">
          <el-form
            :model="ruleForm"
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-form-item label="商品分类" prop="goods_cat">
              <el-cascader
                v-model="ruleForm.goods_cat"
                :options="options"
                :props="props"
              ></el-cascader>
            </el-form-item>
            <el-form-item label="商品名称" prop="goods_name">
              <el-input v-model="ruleForm.goods_name"></el-input>
            </el-form-item>
            <el-form-item label="商品价格" prop="goods_price">
              <el-input v-model="ruleForm.goods_price" type="number"></el-input>
            </el-form-item>
            <el-form-item label="商品重量" prop="goods_weight">
              <el-input
                v-model="ruleForm.goods_weight"
                type="number"
              ></el-input>
            </el-form-item>

            <el-form-item label="商品数量" prop="goods_number">
              <el-input
                v-model="ruleForm.goods_number"
                type="number"
              ></el-input>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="商品参数">
          <myattr :attrvalss="attrvalss"></myattr>
        </el-tab-pane>
        <el-tab-pane label="商品属性">
          <!-- <div v-for="(item, index) in catelist" :key="item.attr_id">
            <p style="margin: 10px 0px">{{ item.attr_name }}</p>
            <el-checkbox-group v-model="item.checked" size="small">
              <el-checkbox
                :label="item1"
                border
                v-for="(item1, index1) in item.attr_vals"
                :key="index1"
              ></el-checkbox>
            </el-checkbox-group>
          </div> -->
          <myattr :attrvalss="catelist"></myattr>
        </el-tab-pane>

        <el-tab-pane label="商品图片">
          <!-- <el-upload
            action=""
            ref="upload"
            :http-request="httpRequest"
            list-type="picture-card"
            :auto-upload="false"
            :file-list="fileList"
            :on-change="handlePictureCardPreview"
          >
            <i class="el-icon-plus"></i>
          </el-upload> -->

          <el-upload
            class="upload-demo"
            action="http://api.wangxiansen.cloud/api/private/v1/upload"
            :headers="headers"
            :file-list="fileList"
            :on-success="handlePictureCardPreview"
            list-type="picture"
          >
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">
              只能上传jpg/png文件，且不超过500kb
            </div>
          </el-upload>
        </el-tab-pane>

        <el-tab-pane label="商品内容">
          <vue-editor v-model="ruleForm.goods_introduce" />
          <el-button type="primary" @click="submitForm('ruleForm')"
            >添加商品</el-button
          >
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
import myattr from "../components/myattr";
import {
  Categories,
  getCate,
  getMany,
  Goodsedit,
  getAdd,
  getUpload,
} from "../utils/http";
import { VueEditor } from "vue2-editor";
import _ from "lodash";
export default {
  name: "goodsadd",
  components: {
    VueEditor,
    myattr,
  },
  data() {
    return {
      active: "0",
      ruleForm: {
        goods_cat: [],
        goods_name: "3",
        goods_price: "1",
        goods_weight: "1",
        goods_number: "1",
        pics: [], //图片路径
        goods_introduce: "", //详细内容富文本
        arrts: [],
      },
      rules: {
        goods_name: [
          { required: true, message: "请输入商品名称", trigger: "blur" },
        ],
        goods_weight: [
          { required: true, message: "请输入商品重量", trigger: "blur" },
        ],
        goods_number: [
          { required: true, message: "请输入商品数量", trigger: "blur" },
        ],
        goods_cat: [
          { required: true, message: "请选择商品分类", trigger: "change" },
        ],
      },
      options: [],
      pageinfo: {
        type: [], //默认获取所有级别的分类
      },
      props: {
        expandTrigger: "hover",
        value: "cat_id",
        label: "cat_name",
        children: "children",
      },
      checked1: true,
      attrvalss: [],
      catelist: [],
      fileList: [], //图片上传的文件列表
      headers: {
        Authorization: localStorage.getItem("token"),
      },
    };
  },
  created() {
    Categories(this.pageinfo).then((res) => {
      // console.log(res);
      this.options = res.data;
    });
  },
  methods: {
    beforeLeave(to, from) {
      if (from == 0 && this.ruleForm.goods_cat.length < 3) {
        this.$message({
          message: "请填写分类",
          type: "error",
        });
        return false;
      }
      if (to == 1 && this.attrvalss.length == 0) {
        getMany({ id: this.ruleForm.goods_cat[2] }).then((res) => {
          res.data.forEach((item) => {
            let arrt = item.attr_vals.trim().split(" ");
            item.attr_vals = arrt;
            item.checked = _.cloneDeep(arrt);
          });
          this.attrvalss = res.data; //静态参数
        });
      }
      if (to == 2 && this.catelist.length == 0) {
        getCate({ id: this.ruleForm.goods_cat[2] }).then((res) => {
          console.log(res);
          res.data.forEach((item) => {
            let arrtt = item.attr_vals.trim().split(" ");
            item.attr_vals = arrtt;
            item.checked = _.cloneDeep(arrtt);
          });
          this.catelist = res.data;
        });
      }
      // console.log(this.active);
    },
    //文件上传钩子
    handlePictureCardPreview(res, file, filelist) {
      console.log(res);
      if (res.meta.status == 200) {
        this.$message({
          type: "success",
          message: res.meta.msg,
        });
        this.ruleForm.pics.push({ pic: res.data.tmp_path });
        console.log(this.ruleForm.pics);
      }
    },
    // //上传图片地址
    // httpRequest(e) {
    //   let formData=new FormData()
    //   formData.append('file',e.file)
    //   getUpload(formData).then(res=>{
    //     console.log(res);
    //   })
    // },
    //添加商品
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // alert("submit!");
          //添加商品
          let ruleForm = _.cloneDeep(this.ruleForm);
          ruleForm.goods_cat = ruleForm.goods_cat.join(",");

          let manylist = this.attrvalss.map((item) => {
            return {
              attr_id: item.attr_id,
              attr_value: item.checked.join(" "),
            };
          });
          let onlylist = this.catelist.map((item) => {
            return {
              attr_id: item.attr_id,
              attr_value: item.checked.join(" "),
            };
          });
          ruleForm.arrts = [...manylist, ...onlylist];
          console.log(ruleForm.arrts);
          getAdd(ruleForm).then((res) => {
            console.log(res);
            if (res.meta.status == 201) {
              this.ruleForm = {};
              this.$message({
                type: "success",
                message: res.meta.msg,
              });
              this.$router.push("/goods");
            } else {
              this.$message({
                type: "danger",
                message: res.meta.msg,
              });
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-breadcrumb {
  margin-bottom: 20px;
}
.el-steps {
  margin-top: 20px;
  margin-bottom: 20px;
}
</style>